﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using StudentMgmt.Model;
using StudentMgmt.SQLServerDAL;

namespace StudentMgmt
{
    public partial class frmUserList : Form
    {
        public frmUserList()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void frmUserList_Load(object sender, EventArgs e)
        {
            Department department = new Department();
            IList<DepartmentInfo> depts = department.GetDepartments();
            this.cmbDepartment.DisplayMember = "Department_Name";
            this.cmbDepartment.ValueMember = "Department_Id";
            this.cmbDepartment.DataSource = depts.ToList();

            Classes oclass = new Classes();
            IList<ClassInfo> classes = oclass.GetClassesByDeptId(this.cmbDepartment.SelectedValue.ToString());
            this.cmbClasses.DisplayMember = "ClassName";
            this.cmbClasses.ValueMember = "ClassId";
            this.cmbClasses.DataSource = classes.ToList();
        }

        private void cmbDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            Classes oclass = new Classes();
            IList<ClassInfo> classes = oclass.GetClassesByDeptId(this.cmbDepartment.SelectedValue.ToString());

            this.cmbClasses.DisplayMember = "ClassName";
            this.cmbClasses.ValueMember = "ClassId";
            this.cmbClasses.DataSource = classes.ToList();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string stuNo = this.txtStuNo.Text.Trim();
            string stuName = this.txtStuName.Text.Trim();
            string class_id = this.cmbClasses.SelectedValue.ToString();
            string department_id = this.cmbDepartment.SelectedValue.ToString();

            Student student = new Student();
            DataTable myTable = new DataTable();

            int val = student.QueryStudent(stuNo, stuName, class_id, department_id, out myTable);
            if (val == 1 && myTable.Rows.Count > 0)
            {
                this.dgStudentList.DataSource = myTable;
            }
        }
    }
}
